﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.Sound;

namespace Meow
{
    public class CompClusterBombProperties : CompProperties
    {
        public ThingDef subProjectile;

        public int clusterCount;

        public int clusterRadius = 3;
    }

    public class TorgueProjectileProps : ProjectileProperties
    {
        public ThingDef subProjectile;

        public int clusterCount;

        public int clusterRadius = 1;
    }

    public class Projectile_Torgue_Rocket : Projectile
    {
        public TorgueProjectileProps Props => (TorgueProjectileProps)def.projectile;

        protected override void Impact(Thing hitThing, bool blockedByShield = false)
        {
            Map map = base.Map;
            IntVec3 position = base.Position;
            Map map2 = map;
            float explosionRadius = def.projectile.explosionRadius;
            DamageDef damageDef = def.projectile.damageDef;
            Thing instigator = launcher;
            int damageAmount = base.DamageAmount;
            float armorPenetration = base.ArmorPenetration;
            ThingDef weapon = equipmentDef;
            GenExplosion.DoExplosion(position, map2, explosionRadius, damageDef, instigator, damageAmount, armorPenetration, null, weapon, def, intendedTarget.Thing);
            for (int i = 0; i < Props.clusterCount; i++)
            {
                int maxExclusive = GenRadial.NumCellsInRadius(Props.clusterRadius);
                int num = ((Props.clusterRadius <= 4) ? Rand.Range(0, maxExclusive) : Rand.Range(2, maxExclusive));
                IntVec3 intVec = base.Position + GenRadial.RadialPattern[num];
                Projectile_Explosive projectile_Explosive = (Projectile_Explosive)ThingMaker.MakeThing(Props.subProjectile);
                GenSpawn.Spawn(projectile_Explosive, position, map);
                projectile_Explosive.Launch(this, position.ToVector3(), intVec, intVec, ProjectileHitFlags.All);
            }

            base.Impact(hitThing);
        }
    }




    public class Torgue_Projectile_Explosive : Projectile_Explosive
    {
        private int ticksToDetonation;

        protected override void Impact(Thing hitThing, bool blockedByShield = false)
        {
            Map map = base.Map;
            BattleLogEntry_RangedImpact battleLogEntry_RangedImpact = new BattleLogEntry_RangedImpact(launcher, hitThing, intendedTarget.Thing, equipmentDef, def, targetCoverDef);
            Find.BattleLog.Add(battleLogEntry_RangedImpact);
            if (hitThing != null)
            {
                DamageDef damageDef = def.projectile.damageDef;
                float amount = base.DamageAmount;
                float armorPenetration = base.ArmorPenetration;
                float y = ExactRotation.eulerAngles.y;
                Thing instigator = launcher;
                ThingDef weapon = equipmentDef;
                DamageInfo dinfo = new DamageInfo(damageDef, amount, armorPenetration, y, instigator, null, weapon, DamageInfo.SourceCategory.ThingOrUnknown, intendedTarget.Thing);
                hitThing.TakeDamage(dinfo).AssociateWithLog(battleLogEntry_RangedImpact);
            }
            else
            {
                SoundDefOf.BulletImpact_Ground.PlayOneShot(new TargetInfo(base.Position, map));
            }

            if (def.projectile.explosionDelay == 0)
            {
                GenClamor.DoClamor(this, 2.1f, ClamorDefOf.Impact);
                Explode();
            }
            else
            {
                landed = true;
                ticksToDetonation = def.projectile.explosionDelay;
                GenExplosion.NotifyNearbyPawnsOfDangerousExplosive(this, def.projectile.damageDef, launcher.Faction);
            }
        }

        public override void ExposeData()
        {
            base.ExposeData();
            Scribe_Values.Look(ref ticksToDetonation, "ticksToDetonation", 0);
        }
    }

}
